import { createRouter, createWebHistory } from 'vue-router'
import { useAppStore } from '../stores/app'
import BasicLayout from '../layouts/BasicLayout.vue'

const routes = [
  {
    path: '/',
    component: BasicLayout,
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页', icon: 'home' }
      },
      {
        path: 'database',
        name: 'Database',
        component: () => import('@/views/Database.vue'),
        meta: { title: '数据库管理', icon: 'database' }
      },
      {
        path: 'mcp',
        name: 'MCPService',
        component: () => import('@/views/MCPService.vue'),
        meta: { title: 'MCP 服务', icon: 'api' }
      },
      {
        path: 'api',
        name: 'Api',
        component: () => import('@/views/Api.vue'),
        meta: { title: 'API 管理', icon: 'api' }
      }
    ]
  },
  // 添加通配符路由，将未匹配的路由重定向到首页
  {
    path: '/:pathMatch(.*)*',
    redirect: '/'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫
router.beforeEach(async (to, from, next) => {
  const appStore = useAppStore()
  
  // 设置页面标题
  document.title = `${to.meta.title} - Data API MCP`
  
  // 设置加载状态
  appStore.setLoading(true)
  
  // 检查是否需要认证
  if (to.meta.requiresAuth) {
    // TODO: 添加认证逻辑
  }
  
  next()
})

// 全局后置守卫
router.afterEach(() => {
  const appStore = useAppStore()
  appStore.setLoading(false)
})

export default router